package com.mysteel.seckill.controller;

import com.mysteel.common.utils.R;
import com.mysteel.seckill.service.SecondKillService;
import com.mysteel.seckill.to.SecKillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * ProjectName: mail
 * ClassName: SeckillController
 * Description:
 * date: 2021/04/12/012 9:03
 *
 * @author xiaochao
 */
@Controller
public class SecKillController {

    @Autowired
    private SecondKillService secondKillService;

    /**
     * 返回当前时间的秒杀商品信息
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/current/secKill/sku")
    public R getCurrentSecKillSku() {
        List<SecKillSkuRedisTo> currentSkus = secondKillService.getCurrentSecKillSku();
        return R.ok().put("data", currentSkus);
    }

    @ResponseBody
    @GetMapping("/sku/secKill/{skuId}")
    public SecKillSkuRedisTo getSkuSecKillInfo(@PathVariable("skuId") Long skuId) {
        return secondKillService.getSkuSecKillInfo(skuId);
    }

    @GetMapping("/kill")
    public String secKill(@RequestParam("killId") String killId,
                          @RequestParam("key") String key,
                          @RequestParam("num") Integer num,
                          Model model){
        String orderSn = secondKillService.rushToBuy(killId,key,num);
        model.addAttribute("orderSn",orderSn);

        return "success";

    }
}
